/*
 * @Author: 码上talk|RC
 * @Date: 2020-10-24 15:02:14
 * @LastEditTime: 2021-01-05 16:00:21
 * @LastEditors: 码上talk|RC
 * @Description: 
 * @FilePath: /tacomall-merchant/src/model/goods/item.js
 * @微信:  13680065830
 * @邮箱:  3189482282@qq.com
 * @oops: Just do what I think it is right
 */
import api from '@/api'
export class GoodsItem {
    constructor(item = {}) {
        const { id, goodsId, name, cover, amount } = item
        this.id = id
        this.goodsId = goodsId
        this.cover = cover
        this.name = name
        this.amount = amount
    }
}

GoodsItem.ENUM = {
    init: () => {
        return {
            goodsAttr: []
        }
    },
    load: async (config = {}) => {
        let result = {}
        for (let i in GoodsItem.ENUM.init()) {
            let body = {}
            if (i === 'goodsAttr') {
                const query = {
                    goodsCategoryId: config['goodsCategoryId']
                }
                body['query'] = query
            }
            const res = await api.open.enumLst({ key: i }, body)
            let { status, data } = res
            if (status) {
                result[i] = data.map(j => {
                    let json = {
                        value: j['id'],
                        label: j['name']
                    }
                    if (i === 'goodsAttr') {
                        json = {
                            value: j['id'],
                            label: j['name'],
                            children: j['children'].map(k => {
                                return {
                                    value: k['id'],
                                    label: k['name']
                                }
                            })
                        }
                    }
                    return json
                })
            }
        }
        return result
    }
}

GoodsItem.FORM = {
    init: (type = 'add') => {
        switch (type) {
            case 'add':
                return {
                    name: {
                        type: 'string|min:4|max:12',
                        key: 'name',
                        value: ''
                    },
                    cover: {
                        isRequire: true,
                        type: 'string',
                        key: 'cover',
                        value: ''
                    },
                    attrJson: {
                        type: 'string',
                        key: 'attrJson',
                        value: ''
                    },
                    marketAmount: {
                        type: 'int',
                        key: 'marketAmount',
                        value: 0
                    },
                    amount: {
                        type: 'int',
                        key: 'amount',
                        value: ''
                    },
                    stock: {
                        type: 'int',
                        key: 'stock',
                        value: 1000
                    }
                }
            case 'update':
                return {
                    id: {
                        type: 'int',
                        key: 'id',
                        value: 0
                    },
                    goodsId: {
                        type: 'int',
                        key: 'goodsId',
                        value: 0
                    },
                    name: {
                        type: 'string|min:4|max:12',
                        key: 'name',
                        value: ''
                    },
                    cover: {
                        isRequire: true,
                        type: 'string',
                        key: 'cover',
                        value: ''
                    },
                    attrJson: {
                        type: 'string',
                        key: 'attrJson',
                        value: ''
                    },
                    marketAmount: {
                        type: 'int',
                        key: 'marketAmount',
                        value: 0
                    },
                    amount: {
                        type: 'int',
                        key: 'amount',
                        value: ''
                    },
                    stock: {
                        type: 'int',
                        key: 'stock',
                        value: 1000
                    }
                }
            default:
                break
        }
    }
}